﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class AccountRepository : IAccountRepository
    {
        private JzSportDbContext _dbContext;

        public AccountRepository(JzSportDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public bool Create(Account account)
        {
            if (string.IsNullOrEmpty(_dbContext.Set<Account>()
                .Where(p => p.Name == account.Name)
                .Select(p => p.Name)
                .SingleOrDefault()))
            {
                _dbContext.Set<Account>().Add(account);
            }
            return false;
        }

        public Account Get(int id)
        {
            return _dbContext.Set<Account>().Where(p => p.Id == id).SingleOrDefault();
        }

        public void Update(int id, Account account)
        {
            _dbContext.Entry<Account>(account).State = System.Data.EntityState.Modified;
        }

        public bool Delete(Guid id)
        {
            throw new NotImplementedException();
        }

        public Account GetByPhone(string phone)
        {
            return _dbContext.Set<Account>()
                .Where(p => p.Phone == phone)
                .SingleOrDefault();
        }

        /// <summary>
        /// 通过第三方账号获取账号信息
        /// </summary>
        /// <param name="thirdPartType"></param>
        /// <param name="thirdPartyAccount"></param>
        /// <returns></returns>
        public Account GetByThirdPartyAccount(string thirdPartyType, string thirdPartyAccount)
        {
            return _dbContext.Set<Account>()
                .Where(p => p.ThirdPartyAccount == thirdPartyAccount && p.ThirdPartyType == thirdPartyType)
                .SingleOrDefault();
        }

        public List<Account> Search(int userId, string filter, int start, int limit)
        {
            if (string.IsNullOrEmpty(filter))
            {
                return _dbContext.Set<Account>()
                    .Where(p => p.Id != userId)
                    .OrderBy(p => p.NickName)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
            }
            else
            {
                int intFilter = 0;
                if (int.TryParse(filter, out intFilter))
                {
                    return _dbContext.Set<Account>()
                        .Where(p => p.Id != userId && (p.Name.Contains(filter) || p.NickName.Contains(filter) || p.Phone.Contains(filter)
                        || p.Id == intFilter))
                        .OrderBy(p => p.NickName)
                        .Skip(start)
                        .Take(limit)
                        .ToList();
                }
                else
                {
                    return _dbContext.Set<Account>()
                        .Where(p => p.Id != userId && (p.Name.Contains(filter) || p.NickName.Contains(filter) || p.Phone.Contains(filter)))
                        .OrderBy(p => p.NickName)
                        .Skip(start)
                        .Take(limit)
                        .ToList();
                }
            }
        }

        public int SearchCount(int userId, string filter)
        {
            if (string.IsNullOrEmpty(filter))
            {
                return _dbContext.Set<Account>().Where(p=>p.Id != userId).Select(p => p.Id).Count();
            }
            int intFilter = 0;
            if (int.TryParse(filter, out intFilter))
            {
                return _dbContext.Set<Account>()
                    .Where(p => p.Id != userId && (p.Name.Contains(filter) || p.NickName.Contains(filter) || p.Phone.Contains(filter)
                    || p.Id == intFilter)).Count();
            }
            else
            {
                return _dbContext.Set<Account>()
                    .Where(p => p.Id != userId && (p.Name.Contains(filter) || p.NickName.Contains(filter) || p.Phone.Contains(filter))).Count();
            }
        }
    }
}
